#include "OMSClientConcrete.h"

#include <Windows.h>
#include <string>

#include "soapOMSServiceSoap12Proxy.h"
#include "OMSServiceSoap12.nsmap"

OMSClientConcrete::OMSClientConcrete(void)
{
}


OMSClientConcrete::~OMSClientConcrete(void)
{
}

std::string OMSClientConcrete::GetAnsiUrl(const std::wstring& serviceUrl)
{
	std::string s;

	int nLength = ::WideCharToMultiByte(CP_ACP, NULL, serviceUrl.c_str(), serviceUrl.length(), NULL, 0, NULL, NULL);
	char* szUrl = new char[nLength+1];
	int nResult = ::WideCharToMultiByte(CP_ACP, NULL, serviceUrl.c_str(), serviceUrl.length(), szUrl, nLength, NULL, NULL);
	szUrl[nLength]='\0';
	if(nResult == 0)
		s = "";
	else
		s = szUrl;
	delete[] szUrl;
	return s;
}

LRESULT OMSClientConcrete::GetServiceInfo(const std::wstring& serviceUrl , std::wstring& serviceInfo){
	OMSServiceSoap12Proxy sc;
	std::string AnsiUrl = GetAnsiUrl(serviceUrl);
	////::soap_ssl_client_context(&sc, SOAP_SSL_NO_AUTHENTICATION, NULL, NULL, NULL, NULL, NULL);
	sc.soap_endpoint = AnsiUrl.c_str();
	//UTF-8
	//soap_ssl_client_context(
	//soap_ssl_accept

	_ns1__GetServiceInfo req;
	_ns1__GetServiceInfoResponse res;

	LONG lResult = sc.GetServiceInfo(&req, &res);
	std::wstring serviceInf1 = *(res.GetServiceInfoResult);
	if(lResult == 0)
		serviceInfo = *(res.GetServiceInfoResult);
	return lResult;
}

LRESULT OMSClientConcrete::GetUserInfo(const std::wstring& serviceUrl , const std::wstring& requestData , std::wstring& userInfo){
	OMSServiceSoap12Proxy sc;
	std::string AnsiUrl = GetAnsiUrl(serviceUrl);
	//::soap_ssl_client_context(&sc, SOAP_SSL_NO_AUTHENTICATION, NULL, NULL, NULL, NULL, NULL);
	sc.soap_endpoint = AnsiUrl.c_str();

	_ns1__GetUserInfo req;
	_ns1__GetUserInfoResponse res;

	req.xmsUser = const_cast<std::wstring*>(&requestData);
	LONG lResult = sc.GetUserInfo(&req, &res);
	if(lResult == 0)
		userInfo = *res.GetUserInfoResult;
	return lResult;
}

LRESULT OMSClientConcrete::SendSMS(const std::wstring& serviceUrl , const std::wstring& smsData , std::wstring& responseData){

	OMSServiceSoap12Proxy sc;
	std::string AnsiUrl = GetAnsiUrl(serviceUrl);
	//::soap_ssl_client_context(&sc, SOAP_SSL_NO_AUTHENTICATION, NULL, NULL, NULL, NULL, NULL);
	sc.soap_endpoint = AnsiUrl.c_str();


	_ns1__SendXms req;
	_ns1__SendXmsResponse res;

	req.xmsData = const_cast<std::wstring*>(&smsData);
	LONG lResult = sc.SendXms(&req, &res);
	if(lResult == 0)
		responseData = *res.SendXmsResult;
	return lResult;
}
